package Offer.offer2019;

import java.util.Stack;

/**
 * 用两个栈实现队列
 *
 * @author: leiwenkang02@meituan.com
 * @date: 2019/4/19
 * @time: 12:51
 */
public class Offer07 {

    private Stack<Integer> stack1 = new Stack<>();
    private Stack<Integer> stack2 = new Stack<>();

    public void add(int value) {
        stack1.push(value);
    }

    public int remove() {
        //1.先判断stack2有没有剩余元素
        if (stack2.isEmpty()) {
            //2.先将stack1中的元素全部移到stack2
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        if (stack2.isEmpty()) {
            throw new RuntimeException("元素为空！");
        }
        //3.从stack2中取出元素
        return stack2.pop();
    }
}
